/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Collections;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import olympicgameslogic.*;

/**
 *
 * @author fabio mendonca
 */
public class DialogRankingAthlete extends JDialog {

   private JComboBox editionCombo;
   private JPanel select, psul;
   private ImageIcon img;
   private String tit;
   private JTable tabela1, tabela2;
   ClassLoader cldr = this.getClass().getClassLoader();
   String[] columnNames = {"#", "Flag", "Code", "Name", "Gold", "Silver", "Bronze", "Total"};
   private DefaultTableModel TableModel1, TableModel2;
   private String titulo;
   private JScrollPane painel1, painel2;
   private Object[][] data1, data2;
   private JTabbedPane pCentroTab;

   public DialogRankingAthlete(Frame owner, String titulo) {
      super(owner, "Ranking " + titulo, true);
      this.titulo = titulo;
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      img = new ImageIcon(cldr.getResource("images/extra/favorite.png"));

//aqui vamos definir qual o ranking que será aberto dependendo do que e passado como atributo           
      if (titulo.equalsIgnoreCase("Nation")) {
	 tit = "Nation";
      } else if (titulo.equalsIgnoreCase("Athlete")) {
	 tit = "Athlete";
      } else if (titulo.equalsIgnoreCase("Modality")) {
	 tit = "Modality";
      }

      setLayout(new BorderLayout());

      setBackground(Color.white);
      //    Criação da barra de topo
      JPanel pannorte = new JPanel(new GridLayout(1, 2));
      pannorte.setSize(200, 60);
      pannorte.setBackground(Color.white);

      JLabel background = new JLabel(tit, JLabel.CENTER);
      background.setFont(new Font("Arial", Font.BOLD, 14));
      background.setIcon(img);
      pannorte.add(background);
      add(pannorte, BorderLayout.NORTH);

      select = new JPanel(new FlowLayout(FlowLayout.LEFT));
      JLabel edic = new JLabel("Edition");
      edic.setOpaque(false);
      select.setBackground(Color.white);
      select.setOpaque(false);
      editionCombo = new JComboBox(OlympicGamesGui.ap.getEditions().toArray());
      editionCombo.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) (e.getSource());
	    if (box.getSelectedIndex() > -1) {
	       refreshTable1(false);
	    }
	 }
      });

      select.add(edic);
      select.add(editionCombo);
      JButton bthtml1 = new JButton("HTML Export");
      bthtml1.setFont(new Font("Arial", Font.PLAIN, 8));
      bthtml1.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    refreshTable1(true);
	 }
      });
      select.add(bthtml1);

//tabela1 (1º tab      
      TableModel1 = new DefaultTableModel(data1, columnNames);
      TableModel1.setColumnIdentifiers(columnNames);
      tabela1 = new JTable() {

	 public boolean isCellEditable(int rowIndex, int colIndex) {
	    return false; //Disallow the editing of any cell
	 }

	 public Class getColumnClass(int column) {
	    if (column == 1) {
	       return ImageIcon.class;
	    }
	    return Object.class;
	    //return getValueAt(0, column).getClass();
	 }
      };
      setUpTableData1(false);

      JPanel p1 = new JPanel(new BorderLayout());
      painel1 = new JScrollPane(tabela1);
      p1.add(select, BorderLayout.NORTH);
      p1.add(painel1, BorderLayout.CENTER);

//tabela2 (2º tab)
      TableModel2 = new DefaultTableModel(data2, columnNames);
      TableModel2.setColumnIdentifiers(columnNames);
      tabela2 = new JTable() {

	 public boolean isCellEditable(int rowIndex, int colIndex) {
	    return false; //Disallow the editing of any cell
	 }

	 public Class getColumnClass(int column) {
	    if (column == 1) {
	       return ImageIcon.class;
	    }
	    return Object.class;
	    //return getValueAt(0, column).getClass();
	 }
      };
      setUpTableData2(false);
      JPanel p2 = new JPanel(new BorderLayout());
      painel2 = new JScrollPane(tabela2);
      JButton bthtml2 = new JButton("HTML Export");
      bthtml2.setFont(new Font("Arial", Font.PLAIN, 8));
      bthtml2.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    refreshTable2(true);

	 }
      });
      JPanel select2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      select2.add(bthtml2);
      p2.add(select2, BorderLayout.NORTH);
      p2.add(painel2, BorderLayout.CENTER);

//definiçaõ de tabs carregando os 2 paineis                          
      pCentroTab = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
      pCentroTab.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

      pCentroTab.addTab("Edition", null, p1, "Edition");
      pCentroTab.addTab("History", null, p2, "History");

      add(pCentroTab, BorderLayout.CENTER);



      setSize(460, 500);
      setResizable(false);
      setLocationRelativeTo(null);

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);


   }

   public void showJan() {

      setVisible(true);







   }

   private void setUpTableData1(Boolean export) {

      tabela1.removeAll();
      LinkedList<Athlete> temp_list = new LinkedList<>();
      Edition e = OlympicGamesGui.ap.getEditions().getElement(editionCombo.getSelectedIndex());
      for (Athlete athlete : OlympicGamesGui.ap.getAthletes()) {
	 for (Delegation d : e.getEditionDelegationList()) {
	    if (d.getAthleteList().contains(athlete)) {
	       temp_list.add(athlete);
	    }
	 }
      }


      Collections.sort(temp_list, new Athlete.MedalsWeightedComparator());

      data1 = new Object[temp_list.getSize()][8];
      int i = 0;
      for (Athlete a : temp_list) {
	 data1[i][0] = "" + (i + 1);
	 String img = "images/flags/" + (a.getNation().getCode()) + ".png";
	 if (cldr.getResource(img) == null) {
	    img = "images/extra/btcancel.png";
	 }
	 data1[i][1] = new ImageIcon(cldr.getResource(img));
	 data1[i][2] = a.getNation().getCode();
	 data1[i][3] = a.getName();
	 data1[i][4] = a.getEditionMedals(e, Medal.MEDALTYPE.GOLD);
	 data1[i][5] = a.getEditionMedals(e, Medal.MEDALTYPE.SILVER);
	 data1[i][6] = a.getEditionMedals(e, Medal.MEDALTYPE.BRONZE);
	 data1[i][7] = a.getTotalEditionMedals(e);

	 if (i < 10) {
	    TableModel1.addRow(data1[i]);
	 }
	 i++;
      }

      if (export) {
	 String fr = HtmlExporter.showSaveDialog(DialogRankingAthlete.this);
	 HtmlExporter.gravarHTML(DialogRankingAthlete.this, fr, HtmlExporter.getHtmlRanking(data1, ((Edition) editionCombo.getSelectedItem()).getYear() + " Edition Athlete Ranking"));


      }
      tabela1.setModel(TableModel1);
      DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
      rightRenderer.setHorizontalAlignment(JLabel.CENTER);
      tabela1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
      TableColumn col = tabela1.getColumnModel().getColumn(0);
      col.setPreferredWidth(30);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(1);
      col.setPreferredWidth(30);
      col = tabela1.getColumnModel().getColumn(2);
      col.setPreferredWidth(50);
      col = tabela1.getColumnModel().getColumn(3);
      col.setPreferredWidth(150);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(4);
      col.setPreferredWidth(40);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(5);
      col.setPreferredWidth(40);
      col.setCellRenderer(rightRenderer);
      col = tabela1.getColumnModel().getColumn(6);
      col.setPreferredWidth(40);
      col.setCellRenderer(rightRenderer);
       col = tabela1.getColumnModel().getColumn(7);
      col.setPreferredWidth(40);
      col.setCellRenderer(rightRenderer);
      JTableHeader header = tabela1.getTableHeader();
      header.setBackground(Color.DARK_GRAY);
      header.setFont(new Font("Arial", Font.BOLD, 12));
      header.setForeground(Color.WHITE);
      tabela1.setPreferredScrollableViewportSize(new Dimension(300, 70));
      tabela1.setFillsViewportHeight(true);
      tabela1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      tabela1.getTableHeader().setReorderingAllowed(false);
      tabela1.getTableHeader().setResizingAllowed(false);
   }

   //Historico
   private void setUpTableData2(Boolean export) {


      tabela2.removeAll();
      LinkedList<Athlete> temp_list = OlympicGamesGui.ap.getAthletes();



      Collections.sort(temp_list, new Athlete.MedalsWeightedComparator());

      data2 = new Object[temp_list.getSize()][8];
      int i = 0;
      for (Athlete a : temp_list) {
	 data2[i][0] = "" + (i + 1);
	 String img = "images/flags/" + (a.getNation().getCode()) + ".png";
	 if (cldr.getResource(img) == null) {
	    img = "images/extra/btcancel.png";
	 }
	 data2[i][1] = new ImageIcon(cldr.getResource(img));
	 data2[i][2] = a.getNation().getCode();
	 data2[i][3] = a.getName();
	 data2[i][4] = a.getMedals(Medal.MEDALTYPE.GOLD);
	 data2[i][5] = a.getMedals(Medal.MEDALTYPE.SILVER);
	 data2[i][6] = a.getMedals(Medal.MEDALTYPE.BRONZE);
	 data2[i][7] = a.getTotalMedals();
	 if (i < 10) {
	    TableModel2.addRow(data2[i]);
	 }
	 i++;

      }
      if (export) {
	 String fr = HtmlExporter.showSaveDialog(DialogRankingAthlete.this);
	 HtmlExporter.gravarHTML(DialogRankingAthlete.this, fr, HtmlExporter.getHtmlRanking(data2, "Historical Athlete Ranking"));


      }


      tabela2.setModel(TableModel2);
      DefaultTableCellRenderer rightRenderer2 = new DefaultTableCellRenderer();
      rightRenderer2.setHorizontalAlignment(JLabel.CENTER);
      tabela2.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
      TableColumn col2 = tabela2.getColumnModel().getColumn(0);
      col2.setPreferredWidth(30);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(1);
      col2.setPreferredWidth(30);
      col2 = tabela2.getColumnModel().getColumn(2);
      col2.setPreferredWidth(50);
      col2 = tabela2.getColumnModel().getColumn(3);
      col2.setPreferredWidth(50);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(4);
      col2.setPreferredWidth(150);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(5);
      col2.setPreferredWidth(40);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(6);
      col2.setPreferredWidth(40);
      col2.setCellRenderer(rightRenderer2);
      col2 = tabela2.getColumnModel().getColumn(7);
      col2.setPreferredWidth(40);
      col2.setCellRenderer(rightRenderer2);
      JTableHeader header2 = tabela2.getTableHeader();
      header2.setBackground(Color.DARK_GRAY);
      header2.setFont(new Font("Arial", Font.BOLD, 12));
      header2.setForeground(Color.WHITE);
      tabela2.setPreferredScrollableViewportSize(new Dimension(300, 70));
      tabela2.setFillsViewportHeight(true);
      tabela2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      tabela2.getTableHeader().setReorderingAllowed(false);
      tabela2.getTableHeader().setResizingAllowed(false);
   }

   public void refreshTable1(Boolean export) {
      TableModel1.setRowCount(0);
      tabela1.removeAll();
      setUpTableData1(export);
   }

   public void refreshTable2(Boolean export) {
      TableModel2.setRowCount(0);
      tabela2.removeAll();
      setUpTableData2(export);
   }
}
